package com.xqls.biz.impl;

import com.xqls.bean.*;
import com.xqls.biz.SokectBiz;
import com.xqls.dal.model.DeviceSchema;
import com.xqls.server.dto.FluSoketCommonDto;
import com.xqls.server.dto.recive.*;
import com.xqls.service.*;
import com.xqls.util.CommonResult;
import com.xqls.util.ContextUtil;
import com.xqls.util.DictionaryEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Service
public class SokectBizImpl implements SokectBiz {


    @Value("${deviceSchema}")
    private String deviceSchema;

    @Autowired
    CompanyService companyService;

    @Autowired
    OperationRecordService operationRecordService;


    @Autowired
    private DischargeRealdataService dischanrgeRealdataService;

    @Autowired
    RainwaterDischargeRealdataService rainwaterDischargeRealdataService;
    @Autowired
    SamplingRecordService samplingRecordService;

    @Autowired
    private IntakeRealdataService intakeRealdataService;

    @Autowired
    private FluggasDischargeRealdataService fluggasDischargeRealdataService;

    @Autowired
    PublicStormwaterRealdataService stormwaterService;

    @Autowired
    private OperationRecordService operationService;
    @Autowired
    private FlowRecordService flowService;
    @Autowired
    private DrainFlowRecordService drainflowService;

    /**
     * 阀门实时值上传
     *
     * @return
     */
    @Override
    public void controlApparatusRecive(ControlApparatusDto controlApparatusDto) {
        //根据编号查询设备
        DictionaryEnums dictionaryEnum = getDevictType(controlApparatusDto.getAdress(), controlApparatusDto.getActiveOperationRecordBean());
        try {

            //操作记录保存

            //阀门状态保存至实时数据
            switch (dictionaryEnum) {
                case DISCHARGE:
                    DischargeRealdataBean dischargeRealdataBean = new DischargeRealdataBean();
                    dischargeRealdataBean.setDeviceNo(controlApparatusDto.getAdress());
                    dischargeRealdataBean.setValveState(String.valueOf(controlApparatusDto.getAngle()));
                    saveOrUpdateDischargeRealdata(dischargeRealdataBean);
                    break;

                case RAINWATER:
                    RainwaterDischargeRealdataBean rainwaterDischargeRealdataBean = new RainwaterDischargeRealdataBean();
                    rainwaterDischargeRealdataBean.setDeviceNo(controlApparatusDto.getAdress());
                    if (controlApparatusDto.getAngle() == 0) {
                        rainwaterDischargeRealdataBean.setValveRealtime("关");
                    } else {
                        rainwaterDischargeRealdataBean.setValveRealtime("开");
                    }
                    saveOrUpdateRainwaterDischargeRealdata(rainwaterDischargeRealdataBean);
                    operationValve(controlApparatusDto, DictionaryEnums.RAINWATER.getCode());
            }
        } catch (Exception e) {
            throw e;
        }

    }

    public void operationValve(ControlApparatusDto controlApparatusDto, int code) {
        if (controlApparatusDto.getProcess() == 37) {
            save(controlApparatusDto.getAdress(), code, DictionaryEnums.VALVE_OPERATION.getCode().toString(), "阀门开启", controlApparatusDto.getAdress());
        } else if (controlApparatusDto.getProcess() == 63) {
            save(controlApparatusDto.getAdress(), code, DictionaryEnums.VALVE_OPERATION.getCode().toString(), "阀门关闭", controlApparatusDto.getAdress());
        }
    }



    //下线
    public void channelInactive(String deviceNO) {
        try {
            CommonResult result = companyService.queryByDeviceNo(deviceNO);
            CompanyBean companyBean = (CompanyBean) result.getData();
            OperationRecordBean operationRecordBean = new OperationRecordBean();
            operationRecordBean.setOperationType(DictionaryEnums.NET_WORK.getCode().toString());
            operationRecordBean.setRecordContent("下线");
            operationRecordBean.setRecordTime(new Date());
            operationRecordBean.setEnterpriseNo(deviceNO);
            operationRecordBean.setModularNo(companyBean.getDeviceType());
            operationRecordBean.setOperationSource(deviceNO);
            operationRecordService.save(operationRecordBean);

            DictionaryEnums dictionaryEnum = null;

            dictionaryEnum = DictionaryEnums.find(companyBean.getDeviceType());

            switch (dictionaryEnum) {
                case INTAKE:
                    IntakeRealdataBean intakeRealdataBean = new IntakeRealdataBean();
                    intakeRealdataBean.setDeviceNo(deviceNO);
                    intakeRealdataBean.setNetwork("离线");
                    saveOrUpdateIntakeRealdata(intakeRealdataBean);
                    break;
                case DISCHARGE:
                    DischargeRealdataBean dischargeRealdataBean = new DischargeRealdataBean();
                    dischargeRealdataBean.setDeviceNo(deviceNO);
                    dischargeRealdataBean.setNetwork("离线");
                    saveOrUpdateDischargeRealdata(dischargeRealdataBean);
                    break;
                case RAINWATER:
                    RainwaterDischargeRealdataBean rainwaterDischargeRealdataBean = new RainwaterDischargeRealdataBean();
                    rainwaterDischargeRealdataBean.setDeviceNo(deviceNO);
                    rainwaterDischargeRealdataBean.setNetwork("离线");
                    saveOrUpdateRainwaterDischargeRealdata(rainwaterDischargeRealdataBean);
                    break;
                case STORMWATER:
                    PublicStormwaterRealdataBean publicStormwaterRealdataBean = new PublicStormwaterRealdataBean();
                    publicStormwaterRealdataBean.setDeviceNo(deviceNO);
                    publicStormwaterRealdataBean.setNetwork("离线");
                    saveOrUpdatetPublicStormwaterRealdata(publicStormwaterRealdataBean);
                    break;
                case WASTEGAS:
                    FluggasDischargeRealdataBean fluggasDischargeRealdataBean = new FluggasDischargeRealdataBean();
                    fluggasDischargeRealdataBean.setDeviceNo(deviceNO);
                    fluggasDischargeRealdataBean.setNetwork("离线");
                    saveOrUpdatetFluggasDischargeRealdata(fluggasDischargeRealdataBean);
                    break;
                //废气 // TODO: 2017/7/12   废气实时数据离线保存


            }
        } catch (Exception e) {

        }


    }

    @Override
    public void wastegasRecive(FluSoketCommonDto dto) {
        DictionaryEnums dictionaryEnum = getDevictType(dto.getAdress(), dto.getActiveOperationRecordBean());
        try {
            DateFormat from_type = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            switch (dictionaryEnum) {
                case WASTEGAS:
                    FluggasDischargeRealdataBean fluBean = new FluggasDischargeRealdataBean();
                    fluBean.setNetwork("在线");
                    fluBean.setDeviceNo(dto.getAdress());
                    Map<String, Object> mapTime = new HashMap<>();
                    mapTime = dto.getSimpleMap();
                    Iterator iter = mapTime.entrySet().iterator();
                    while (iter.hasNext()) {
                        Date date = new Date();
                        try {
                            Object key = iter.next();
                            date = from_type.parse(mapTime.get(key).toString());

                        } catch (Exception e) {

                        }
                        fluBean.setRecordTime(date);
                    }
                    String start = "";
                    for (int i = 0; i < dto.getDatas().size(); i++) {
                        String code = dto.getDatas().get(i).getCode();
                        String rtd = dto.getDatas().get(i).getRtd();
                        String flag = dto.getDatas().get(i).getFlag();

                        double rtds = Double.parseDouble(rtd);
                        if ("S01".equals(code)) {
                            fluBean.setO2Content(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setO2ContentState("正常");
                            } else {
                                fluBean.setO2ContentState("故障");
                            }
                        } else if ("S02".equals(code)) {
                            fluBean.setSmokeVelocity(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setSmokeVelocityState("正常");
                            } else {
                                fluBean.setSmokeVelocityState("故障");
                            }
                        } else if ("S03".equals(code)) {
                            fluBean.setSmokeTemperature(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setSmokeTemperatureState("正常");
                            } else {
                                fluBean.setSmokeTemperatureState("故障");
                            }
                        } else if ("S04".equals(code)) {
                            fluBean.setSmokeDynamicPressure(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setSmokeDynamicPressureState("正常");
                            } else {
                                fluBean.setSmokeDynamicPressureState("故障");
                            }
                        } else if ("S05".equals(code)) {
                            fluBean.setSmokeHumidity(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setSmokeHumidityState("正常");
                            } else {
                                fluBean.setSmokeHumidityState("故障");
                            }
                        } else if ("S08".equals(code)) {
                            fluBean.setSmokePressure(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setSmokePressureState("正常");
                            } else {
                                fluBean.setSmokePressureState("故障");
                            }
                        } else if ("001".equals(code)) {
                            fluBean.setSmoke(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setSmokeState("正常");
                            } else {
                                fluBean.setSmokeState("故障");
                            }
                        } else if ("002".equals(code)) {
                            fluBean.setSo2(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setSo2State("正常");
                            } else {
                                fluBean.setSo2State("故障");
                            }
                        } else if ("003".equals(code)) {
                            fluBean.setNo2(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setNo2State("正常");
                            } else {
                                fluBean.setNo2State("故障");
                            }
                        } else if ("004".equals(code)) {
                            fluBean.setCo(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setCoState("正常");
                            } else {
                                fluBean.setCoState("故障");
                            }
                        } else if ("005".equals(code)) {
                            fluBean.setH2s(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setH2sState("正常");
                            } else {
                                fluBean.setH2sState("故障");
                            }
                        } else if ("008".equals(code)) {
                            fluBean.setHcl(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setHclState("正常");
                            } else {
                                fluBean.setHclState("故障");
                            }
                        } else if ("010".equals(code)) {
                            fluBean.setNh3(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setNh3State("正常");
                            } else {
                                fluBean.setNh3State("故障");
                            }
                        } else if ("011".equals(code)) {
                            fluBean.setCl2(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setCl2State("正常");
                            } else {
                                fluBean.setCl2State("故障");
                            }
                        } else if ("012".equals(code)) {
                            fluBean.setCs2(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setCs2State("正常");
                            } else {
                                fluBean.setCs2State("故障");
                            }
                        } else if ("016".equals(code)) {
                            fluBean.setBenzeneSeries(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setBenzeneSeriesState("正常");
                            } else {
                                fluBean.setBenzeneSeriesState("故障");
                            }
                        } else if ("017".equals(code)) {
                            fluBean.setToluene(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setTolueneState("正常");
                            } else {
                                fluBean.setTolueneState("故障");
                            }
                        } else if ("018".equals(code)) {
                            fluBean.setXylene(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setXyleneState("正常");
                            } else {
                                fluBean.setXyleneState("故障");
                            }
                        } else if ("019".equals(code)) {
                            fluBean.setFormaldehyde(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setFormaldehydeState("正常");
                            } else {
                                fluBean.setFormaldehydeState("故障");
                            }
                        } else if ("027".equals(code)) {
                            fluBean.setPhenols(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setPhenolsState("正常");
                            } else {
                                fluBean.setPhenolsState("故障");
                            }
                        } else if ("028".equals(code)) {
                            fluBean.setMethanol(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setMethanolState("正常");
                            } else {
                                fluBean.setMethanolState("故障");
                            }
                        } else if ("029".equals(code)) {
                            fluBean.setVinylChloride(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setVinylChlorideState("正常");
                            } else {
                                fluBean.setVinylChlorideState("故障");
                            }
                        } else if ("030".equals(code)) {
                            fluBean.setCo2(rtds);
                            if ("N".equals(flag) || "B".equals(flag) || "D".equals(flag) || "F".equals(flag)) {
                                fluBean.setCo2State("正常");
                            } else {
                                fluBean.setCo2State("故障");
                            }
                        }

                        if ("N".equals(dto.getDatas().get(0).getFlag()) || "A".equals(dto.getDatas().get(0).getFlag()) || "B".equals(dto.getDatas().get(0).getFlag()) || "C".equals(dto.getDatas().get(0).getFlag())) {
                            fluBean.setState("关");
                        } else {
                            fluBean.setState("开");
                        }
                        if ("N".equals(dto.getDatas().get(0).getFlag()) || "A".equals(dto.getDatas().get(0).getFlag()) || "D".equals(dto.getDatas().get(0).getFlag()) || "E".equals(dto.getDatas().get(0).getFlag())) {
                            fluBean.setPowerSupply("正常");
                        } else {
                            fluBean.setPowerSupply("异常");
                        }

                    }
                    saveOrUpdatetFluggasDischargeRealdata(fluBean);
                    break;
            }
        } catch (Exception e) {
            throw e;
        }

    }


    private DictionaryEnums getDevictType(String deviceNo, OperationRecordBean operationRecordBean) {
        DictionaryEnums dictionaryEnum = null;
        //保存上线的操作记录

        CommonResult result = companyService.queryByDeviceNo(deviceNo);
        if (result.getIsSuccess()) {
            CompanyBean companyBean = (CompanyBean) result.getData();
            if (operationRecordBean != null) {
                operationRecordBean.setEnterpriseNo(deviceNo);
                operationRecordBean.setModularNo(companyBean.getDeviceType());
                operationRecordBean.setOperationSource(deviceNo);
                operationRecordService.save(operationRecordBean);
            }
            dictionaryEnum = DictionaryEnums.find(companyBean.getDeviceType());
            switch (dictionaryEnum) {
                case INTAKE:
                    IntakeRealdataBean intakeRealdataBean = new IntakeRealdataBean();
                    intakeRealdataBean.setDeviceNo(deviceNo);
                    intakeRealdataBean.setNetwork("在线");
                    saveOrUpdateIntakeRealdata(intakeRealdataBean);
                    break;
                case DISCHARGE:
                    DischargeRealdataBean dischargeRealdataBean = new DischargeRealdataBean();
                    dischargeRealdataBean.setDeviceNo(deviceNo);
                    dischargeRealdataBean.setNetwork("在线");
                    saveOrUpdateDischargeRealdata(dischargeRealdataBean);
                    break;
                case RAINWATER:
                    RainwaterDischargeRealdataBean rainwaterDischargeRealdataBean = new RainwaterDischargeRealdataBean();
                    rainwaterDischargeRealdataBean.setDeviceNo(deviceNo);
                    rainwaterDischargeRealdataBean.setNetwork("在线");
                    saveOrUpdateRainwaterDischargeRealdata(rainwaterDischargeRealdataBean);
                    break;
                case STORMWATER:
                    PublicStormwaterRealdataBean publicStormwaterRealdataBean = new PublicStormwaterRealdataBean();
                    publicStormwaterRealdataBean.setDeviceNo(deviceNo);
                    publicStormwaterRealdataBean.setNetwork("在线");
                    saveOrUpdatetPublicStormwaterRealdata(publicStormwaterRealdataBean);
                    break;
                case WASTEGAS:
                    FluggasDischargeRealdataBean fluggasDischargeRealdataBean = new FluggasDischargeRealdataBean();
                    fluggasDischargeRealdataBean.setDeviceNo(deviceNo);
                    fluggasDischargeRealdataBean.setNetwork("在线");
                    saveOrUpdatetFluggasDischargeRealdata(fluggasDischargeRealdataBean);
                    break;
                //废气 // TODO: 2017/7/12   废气实时数据上线保存


            }
        }
        if (dictionaryEnum == null) {
            dictionaryEnum = DictionaryEnums.UNKNOW;
        }


        return dictionaryEnum;
    }


    /**
     * 采样仪采样记录
     *
     * @return
     */
    public void samplingRecive(SamplingDto samplingDto) {
        //保存采样记录 记录操作记录
        //根据编号查询设备
        DictionaryEnums dictionaryEnum = getDevictType(samplingDto.getAdress(), samplingDto.getActiveOperationRecordBean());

        try {
//采样仪采样记录保存至采样记录
            //实时记录数据保存
            switch (dictionaryEnum) {
                case INTAKE:
                    //实时数据
                    IntakeRealdataBean intakeRealdataBean = new IntakeRealdataBean();
                    intakeRealdataBean.setDeviceNo(samplingDto.getAdress());
                    if (samplingDto.getPowerStatus() == 0) {
                        intakeRealdataBean.setPowerSupply("正常");
                    } else {
                        intakeRealdataBean.setPowerSupply("异常");
                    }
                    saveOrUpdateIntakeRealdata(intakeRealdataBean);
                    save(samplingDto.getAdress(), DictionaryEnums.POWER_SUPPLY.getCode(), DictionaryEnums.INTAKE.getCode().toString(), intakeRealdataBean.getPowerSupply(), samplingDto.getAdress());
                    break;
                case DISCHARGE:
                    //实时数据
                    DischargeRealdataBean dischargeRealdataBean = new DischargeRealdataBean();
                    dischargeRealdataBean.setDeviceNo(samplingDto.getAdress());
                    if (samplingDto.getLevel() == 0) {
                        // 0代表无液位,停止排水;1代表有液位,开始排水
                        dischargeRealdataBean.setDrainLevel("异常");
                    } else {
                        dischargeRealdataBean.setDrainLevel("正常");
                    }
                    //dischargeRealdataBean.setCod(samplingDto.getUV()+"");
                    //dischargeRealdataBean.setSs(samplingDto.getSS().toString());
                    //dischargeRealdataBean.setNh3(samplingDto.getNH3()+"");
                    //dischargeRealdataBean.setPh(samplingDto.getPH()+"");
                    //dischargeRealdataBean.setIceboxTemperature(samplingDto.getTemperature().toString());
                    dischargeRealdataBean.setBottleNo(samplingDto.getNumber());
                    dischargeRealdataBean.setBottleNumber(samplingDto.getFrequency().toString());
                    dischargeRealdataBean.setSampleRetention(samplingDto.getMeasure() + 0d);
                    if (samplingDto.getInletStatus() == 0) {
                        dischargeRealdataBean.setInlet("正常");
                    } else {
                        dischargeRealdataBean.setInlet("异常");
                    }
                    if (samplingDto.getValveStatus() == 0) {
                        dischargeRealdataBean.setInstrument("关");
                    } else {
                        dischargeRealdataBean.setInstrument("开");
                    }
                    if (samplingDto.getPowerStatus() == 0) {
                        dischargeRealdataBean.setPowerSupply("正常");
                    } else {
                        dischargeRealdataBean.setPowerSupply("异常");
                    }
                    if (samplingDto.getStartUp() == 0) {
                        dischargeRealdataBean.setSamplingState("空闲");
                    } else {
                        dischargeRealdataBean.setSamplingState("启动");
                    }
                    dischargeRealdataBean.setSamplingTemperature(samplingDto.getAccumulativeTotal().toString());
                    //采样记录
                    SamplingRecordBean recordBean = new SamplingRecordBean();
                    recordBean.setDeviceNo(samplingDto.getAdress());
                    recordBean.setSamplingTemperature(samplingDto.getAccumulativeTotal().toString());
                    recordBean.setBottleNo(samplingDto.getNumber());
                    recordBean.setSampleType("discharge");
                    recordBean.setRecordingTime(samplingDto.getTime());
                    recordBean.setSampleRetention(samplingDto.getMeasure() + 0d);
                    recordBean.setPh(samplingDto.getPH() + 0d);
                    recordBean.setNh3(samplingDto.getNH3() + "");
                    recordBean.setCod(samplingDto.getUV() + 0d);
                    recordBean.setSs(samplingDto.getSS().toString());
                    recordBean.setOperationSource(samplingDto.getAdress());
                    recordBean.setIceboxTemperature(samplingDto.getTemperature().toString());
                    recordBean.setBottleNumber(samplingDto.getFrequency().toString());
                    saveOrUpdateDischargeRealdata(dischargeRealdataBean);
                    samplingRecordService.insertSamplingRecord(recordBean);
                    operationLevelSamDoor(samplingDto, DictionaryEnums.DISCHARGE.getCode());
                    //电源保存
                    save(samplingDto.getAdress(), DictionaryEnums.POWER_SUPPLY.getCode(), DictionaryEnums.DISCHARGE.getCode().toString(), dischargeRealdataBean.getPowerSupply(), samplingDto.getAdress());


                    break;
                case RAINWATER:
                    //实时数据
                    RainwaterDischargeRealdataBean rainwaterDischargeRealdataBean = new RainwaterDischargeRealdataBean();
                    rainwaterDischargeRealdataBean.setDeviceNo(samplingDto.getAdress());
                    //rainwaterDischargeRealdataBean.setCod(samplingDto.getUV()+"");
                    //rainwaterDischargeRealdataBean.setSs(samplingDto.getSS().toString());
                    //rainwaterDischargeRealdataBean.setNh3(samplingDto.getNH3()+"");
                    //rainwaterDischargeRealdataBean.setPh(samplingDto.getPH()+"");
                    //冰箱温度
                    rainwaterDischargeRealdataBean.setIceboxTemperature(samplingDto.getTemperature().toString());
                    //采样瓶号
                    rainwaterDischargeRealdataBean.setBottleNo(samplingDto.getNumber());
                    //采样瓶次
                    rainwaterDischargeRealdataBean.setBottleNumber(samplingDto.getFrequency().toString());
                    //采样量
                    rainwaterDischargeRealdataBean.setSampleRetention(samplingDto.getMeasure() + 0d);
                    if (samplingDto.getStartUp() == 0) {//仪器启动
                        rainwaterDischargeRealdataBean.setSamplingState("空闲");
                    } else {
                        rainwaterDischargeRealdataBean.setSamplingState("启动");
                    }
                    if (samplingDto.getValveStatus() == 0) {//阀门状态
                        rainwaterDischargeRealdataBean.setInstrument("关");
                    } else {
                        rainwaterDischargeRealdataBean.setInstrument("开");
                    }
                    if (samplingDto.getInletStatus() == 0) {//进水状态
                        rainwaterDischargeRealdataBean.setInlet("正常");
                    } else {
                        rainwaterDischargeRealdataBean.setInlet("异常");
                    }
                    if (samplingDto.getPowerStatus() == 0) {//电源状态
                        rainwaterDischargeRealdataBean.setPowerSupply("正常");
                    } else {
                        rainwaterDischargeRealdataBean.setPowerSupply("异常");
                    }
                    if (samplingDto.getLevel() == 0) {//液位信号
                        rainwaterDischargeRealdataBean.setDrainLevel("异常");
                    } else {
                        rainwaterDischargeRealdataBean.setDrainLevel("正常");
                    }
                    rainwaterDischargeRealdataBean.setSamplingTemperature(samplingDto.getAccumulativeTotal().toString());
                    saveOrUpdateRainwaterDischargeRealdata(rainwaterDischargeRealdataBean);
                    //采样记录
                    SamplingRecordBean rainwaterBean = new SamplingRecordBean();
                    rainwaterBean.setDeviceNo(samplingDto.getAdress());
                    rainwaterBean.setBottleNo(samplingDto.getNumber());
                    rainwaterBean.setSampleType("rainwater");
                    rainwaterBean.setRecordingTime(samplingDto.getTime());
                    rainwaterBean.setSampleRetention(samplingDto.getMeasure() + 0d);
                    rainwaterBean.setPh(samplingDto.getPH() + 0d);
                    rainwaterBean.setNh3(samplingDto.getNH3() + "");
                    rainwaterBean.setCod(samplingDto.getUV() + 0d);
                    rainwaterBean.setOperationSource(samplingDto.getAdress());
                    rainwaterBean.setIceboxTemperature(samplingDto.getTemperature().toString());
                    rainwaterBean.setBottleNumber(samplingDto.getFrequency().toString());
                    rainwaterBean.setSamplingTemperature(samplingDto.getAccumulativeTotal().toString());
                    samplingRecordService.insertSamplingRecord(rainwaterBean);
                    operationLevelSamDoor(samplingDto, DictionaryEnums.RAINWATER.getCode());
                    save(samplingDto.getAdress(), DictionaryEnums.SAMPLING_INSTRUMENT.getCode(), DictionaryEnums.RAINWATER.getCode().toString(), "企业雨水排放口采样记录保存", samplingDto.getAdress());
                    break;
                case STORMWATER:
                    //实时数据
                    PublicStormwaterRealdataBean publicStormwaterRealdataBean = new PublicStormwaterRealdataBean();
                    publicStormwaterRealdataBean.setDeviceNo(samplingDto.getAdress());
                    if (samplingDto.getLevel() == 0) {//液位信号
                        publicStormwaterRealdataBean.setDrainLevel("未排水");
                    } else {
                        publicStormwaterRealdataBean.setDrainLevel("排水");
                    }
                    if (samplingDto.getStartUp() == 0) {//仪器是否启动
                        publicStormwaterRealdataBean.setState("空闲");
                    } else {
                        publicStormwaterRealdataBean.setState("启动");
                    }
                    if (samplingDto.getInletStatus() == 0) {//进水状态
                        publicStormwaterRealdataBean.setInlet("正常");
                    } else {
                        publicStormwaterRealdataBean.setInlet("异常");
                    }
                    if (samplingDto.getValveStatus() == 0) {//仪器门状态
                        publicStormwaterRealdataBean.setInstrument("关");
                    } else {
                        publicStormwaterRealdataBean.setInstrument("开");
                    }
                    if (samplingDto.getPowerStatus() == 0) {//电源状态
                        publicStormwaterRealdataBean.setPowerSupply("正常");
                    } else {
                        publicStormwaterRealdataBean.setPowerSupply("异常");
                    }
                    //publicStormwaterRealdataBean.setCod(samplingDto.getUV()+0d);
                    //publicStormwaterRealdataBean.setSs(samplingDto.getSS().toString());
                    //publicStormwaterRealdataBean.setNh3(samplingDto.getNH3()+"");
                    //publicStormwaterRealdataBean.setPh(samplingDto.getPH()+0d);
                    //冰箱温度
                    publicStormwaterRealdataBean.setIceboxTemperature(samplingDto.getTemperature().toString());
                    //采样瓶号
                    publicStormwaterRealdataBean.setBottleNo(samplingDto.getNumber());
                    //采样瓶次
                    publicStormwaterRealdataBean.setBottleNumber(samplingDto.getFrequency().toString());
                    //采样量
                    publicStormwaterRealdataBean.setSampleRetention(samplingDto.getMeasure() + 0d);
                    publicStormwaterRealdataBean.setSamplingTemperature(samplingDto.getAccumulativeTotal().toString());
                    saveOrUpdatetPublicStormwaterRealdata(publicStormwaterRealdataBean);

                    //采样记录
                    SamplingRecordBean stormwaterBean = new SamplingRecordBean();
                    stormwaterBean.setDeviceNo(samplingDto.getAdress());
                    stormwaterBean.setBottleNo(samplingDto.getNumber());
                    stormwaterBean.setSampleType("stormwater");
                    stormwaterBean.setRecordingTime(samplingDto.getTime());
                    stormwaterBean.setSampleRetention(samplingDto.getMeasure() + 0d);
                    stormwaterBean.setPh(samplingDto.getPH() + 0d);
                    stormwaterBean.setNh3(samplingDto.getNH3() + "");
                    stormwaterBean.setCod(samplingDto.getUV() + 0d);
                    stormwaterBean.setOperationSource(samplingDto.getAdress());
                    stormwaterBean.setIceboxTemperature(samplingDto.getTemperature().toString());
                    stormwaterBean.setBottleNumber(samplingDto.getFrequency().toString());
                    stormwaterBean.setSamplingTemperature(samplingDto.getAccumulativeTotal().toString());
                    samplingRecordService.insertSamplingRecord(stormwaterBean);
                    operationLevelSamDoor(samplingDto, DictionaryEnums.STORMWATER.getCode());
                    save(samplingDto.getAdress(), DictionaryEnums.SAMPLING_INSTRUMENT.getCode(), DictionaryEnums.STORMWATER.getCode().toString(), "公共雨水排放口采样记录保存", samplingDto.getAdress());
                    //实时数据
            }
        } catch (Exception e) {
            throw e;
        }


        //操作记录保存采样仪数据
    }

    /**
     * 公共的  采样仪、仪表门、液位的操作记录保存方法
     *
     * @param samplingDto 接口接收到的
     * @param code        对应的枚举类型
     */
    public void operationLevelSamDoor(SamplingDto samplingDto, int code) {
        if (samplingDto.getInletStatus() == 0) {
            save(samplingDto.getAdress(), code, DictionaryEnums.SAMPLING_INSTRUMENT.getCode().toString(), "进水成功", samplingDto.getAdress());
        } else {
            save(samplingDto.getAdress(), code, DictionaryEnums.SAMPLING_INSTRUMENT.getCode().toString(), "进水失败", samplingDto.getAdress());
        }
        if (samplingDto.getValveStatus() == 0) {
            save(samplingDto.getAdress(), code, DictionaryEnums.INSTRUMENT_DOOR.getCode().toString(), "关门", samplingDto.getAdress());
        } else {
            save(samplingDto.getAdress(), code, DictionaryEnums.INSTRUMENT_DOOR.getCode().toString(), "开门", samplingDto.getAdress());
        }
        if (samplingDto.getLevel() == 0) {
            save(samplingDto.getAdress(), code, DictionaryEnums.LEVEL.getCode().toString(), "无液位，停止排水", samplingDto.getAdress());
        } else {
            save(samplingDto.getAdress(), code, DictionaryEnums.LEVEL.getCode().toString(), "有液位，开始排水", samplingDto.getAdress());
        }
    }


    /**
     * 采样仪水质因子实时值
     *
     * @return
     */
    public void samplingRealTimeRecive(SamplingRealTimeDto samplingRealTimeDto) {
        DictionaryEnums dictionaryEnum = getDevictType(samplingRealTimeDto.getAdress(), samplingRealTimeDto.getActiveOperationRecordBean());
        try {
//累计流量保存至实时数据
            switch (dictionaryEnum) {
                case INTAKE:
                    //取水口 保存实时记录
                    IntakeRealdataBean intakeRealdataBean = new IntakeRealdataBean();
                    intakeRealdataBean.setDeviceNo(samplingRealTimeDto.getAdress());
                    intakeRealdataBean.setInstantaneousFlow(samplingRealTimeDto.getInstantaneousMeasure() + 0d);
                    intakeRealdataBean.setCumulativeFlow(samplingRealTimeDto.getAccumulativeTotal() + 0d);
                    saveOrUpdateIntakeRealdata(intakeRealdataBean);
                    // flow_record 保存
                    save(samplingRealTimeDto.getAccumulativeTotal() + 0d, samplingRealTimeDto.getAdress(), samplingRealTimeDto.getInstantaneousMeasure() + 0d, samplingRealTimeDto.getTime());
                    break;
                case DISCHARGE:
                    DischargeRealdataBean dischargeRealdataBean = new DischargeRealdataBean();
                    dischargeRealdataBean.setDeviceNo(samplingRealTimeDto.getAdress());
                    //瞬时流量
                    dischargeRealdataBean.setInstantaneousFlow(samplingRealTimeDto.getInstantaneousMeasure() + 0d);
                    //UV
                    dischargeRealdataBean.setCod(samplingRealTimeDto.getUV() + "");
                    //SS
                    dischargeRealdataBean.setSs(samplingRealTimeDto.getSS().toString());
                    //PH
                    dischargeRealdataBean.setPh(samplingRealTimeDto.getPH().toString());
                    //氨氮
                    dischargeRealdataBean.setNh3(samplingRealTimeDto.getAmmoniaValue().toString());
                    //水泵电流
                    dischargeRealdataBean.setPumpCurrent(samplingRealTimeDto.getElectricCurrent() + "");
                    //实时温度
                    dischargeRealdataBean.setRealTemperature(samplingRealTimeDto.getTemperature() + "");
                    //累计流量
                    dischargeRealdataBean.setCumulativeFlow(samplingRealTimeDto.getAccumulativeTotal() + 0d);
                    saveOrUpdateDischargeRealdata(dischargeRealdataBean);
                    //  排水口的 flow_record 保存
                    drainSave(samplingRealTimeDto.getAccumulativeTotal() + 0d, samplingRealTimeDto.getAdress(), samplingRealTimeDto.getInstantaneousMeasure() + 0d, samplingRealTimeDto.getTime());
                    break;
                case RAINWATER:
                    RainwaterDischargeRealdataBean rainwaterDischargeRealdataBean = new RainwaterDischargeRealdataBean();
                    rainwaterDischargeRealdataBean.setDeviceNo(samplingRealTimeDto.getAdress());
                    //水泵电流
                    rainwaterDischargeRealdataBean.setPumpCurrent(samplingRealTimeDto.getElectricCurrent() + 0d);
                    //瞬时流量
                    rainwaterDischargeRealdataBean.setInstantaneousFlow(samplingRealTimeDto.getInstantaneousMeasure() + 0d);
                    //UV
                    rainwaterDischargeRealdataBean.setCod(samplingRealTimeDto.getUV() + "");
                    //SS
                    rainwaterDischargeRealdataBean.setSs(samplingRealTimeDto.getSS().toString());
                    //PH
                    rainwaterDischargeRealdataBean.setPh(samplingRealTimeDto.getPH().toString());
                    //氨氮
                    rainwaterDischargeRealdataBean.setNh3N(samplingRealTimeDto.getAmmoniaValue().toString());
                    //实时温度
                    rainwaterDischargeRealdataBean.setRealTemperature(samplingRealTimeDto.getTemperature() + "");
                    //累计流量
                    rainwaterDischargeRealdataBean.setCumulativeFlow(samplingRealTimeDto.getAccumulativeTotal() + 0d);
                    saveOrUpdateRainwaterDischargeRealdata(rainwaterDischargeRealdataBean);
                    operationEmergency_pump(samplingRealTimeDto, DictionaryEnums.RAINWATER.getCode());
                    break;
                case STORMWATER:
                    PublicStormwaterRealdataBean publicStormwaterRealdataBean = new PublicStormwaterRealdataBean();
                    publicStormwaterRealdataBean.setDeviceNo(samplingRealTimeDto.getAdress());
                    publicStormwaterRealdataBean.setPh(samplingRealTimeDto.getPH() + 0d);
                    publicStormwaterRealdataBean.setCod(samplingRealTimeDto.getUV() + 0d);
                    //水泵电流
                    publicStormwaterRealdataBean.setPumpCurrent(samplingRealTimeDto.getElectricCurrent() + "");
                    //瞬时流量
                    publicStormwaterRealdataBean.setInstantaneousFlow(samplingRealTimeDto.getInstantaneousMeasure() + 0d);
                    //SS
                    publicStormwaterRealdataBean.setSs(samplingRealTimeDto.getSS().toString());
                    //氨氮
                    publicStormwaterRealdataBean.setNh3(samplingRealTimeDto.getAmmoniaValue().toString());
                    //实时温度
                    publicStormwaterRealdataBean.setRealTemperature(samplingRealTimeDto.getTemperature() + "");
                    //累计流量
                    publicStormwaterRealdataBean.setCumulativeFlow(samplingRealTimeDto.getAccumulativeTotal() + 0d);
                    saveOrUpdatetPublicStormwaterRealdata(publicStormwaterRealdataBean);
                    operationEmergency_pump(samplingRealTimeDto, DictionaryEnums.STORMWATER.getCode());
            }
        } catch (Exception e) {
            throw e;
        }


    }

    public void operationEmergency_pump(SamplingRealTimeDto samplingRealTimeDto, int code) {
        if (samplingRealTimeDto.getElectricCurrent() == null) {
            save(samplingRealTimeDto.getAdress(), code, DictionaryEnums.EMERGENCY_PUMP.getCode().toString(), "应急池水泵关", samplingRealTimeDto.getAdress());
        } else {
            save(samplingRealTimeDto.getAdress(), code, DictionaryEnums.EMERGENCY_PUMP.getCode().toString(), "应急池水泵开，水泵电流为" + samplingRealTimeDto.getElectricCurrent() + "A", samplingRealTimeDto.getAdress());
        }
    }

    /**
     * 采样仪采样设置采样仪返回
     *
     * @return
     */
    public void samplingSetBack(SamplingSetBackDto samplingSetBackDto) {
        //根据编号查询设备
        DictionaryEnums dictionaryEnum = getDevictType(samplingSetBackDto.getAdress(), null);
        try {
            //// TODO: 2017/3/14  采样设置回调 记录操作记录
            save(samplingSetBackDto.getAdress(), DictionaryEnums.SAMPLING_INSTRUMENT.getCode(), DictionaryEnums.STORMWATER.getCode().toString(), "采样仪采样设置采样仪返回", samplingSetBackDto.getAdress());
        } catch (Exception e) {
            throw e;
        }


    }


    /**
     * 开关阀门指令发送返回
     *
     * @return
     */
    public void sendSwitchValveBack(SwitchValveBackDto switchValveBackDto) {
        DictionaryEnums dictionaryEnum = getDevictType(switchValveBackDto.getAdress(), null);
        try {
            //// TODO: 2017/3/14  阀门指令回调 记录操作记录
            save(switchValveBackDto.getAdress(), DictionaryEnums.INSTRUMENT_DOOR.getCode(), DictionaryEnums.STORMWATER.getCode().toString(), "开关阀门指令发送返回", switchValveBackDto.getAdress());

        } catch (Exception e) {
            throw e;
        }

    }

    //intakeRealdataService
    private void saveOrUpdateIntakeRealdata(IntakeRealdataBean intakeRealdataBean) {
        IntakeRealdataBean realdataBean = intakeRealdataService.queryByDeviceNo(intakeRealdataBean.getDeviceNo());
        if (realdataBean == null) {
            intakeRealdataService.insertIntakeRealdata(intakeRealdataBean);
        } else {
            intakeRealdataBean.setRealdataNo(realdataBean.getRealdataNo());
            intakeRealdataService.updateIntakeRealdata(intakeRealdataBean);
        }
    }

    private void saveOrUpdateDischargeRealdata(DischargeRealdataBean dischargeRealdataBean) {
        DischargeRealdataBean realdataBean = dischanrgeRealdataService.queryByDeviceNo(dischargeRealdataBean.getDeviceNo());
        if (realdataBean == null) {
            dischanrgeRealdataService.insertDischargeRealdata(dischargeRealdataBean);
        } else {
            dischargeRealdataBean.setRealdataNo(realdataBean.getRealdataNo());
            dischanrgeRealdataService.updateDischargeRealdata(dischargeRealdataBean);
        }
    }

    private void saveOrUpdateRainwaterDischargeRealdata(RainwaterDischargeRealdataBean rainwaterDischargeRealdataBean) {
        RainwaterDischargeRealdataBean realdataBean = rainwaterDischargeRealdataService.queryByDeviceNo(rainwaterDischargeRealdataBean.getDeviceNo());
        if (realdataBean == null) {
            rainwaterDischargeRealdataService.insertRainwaterDischargeRealdataList(rainwaterDischargeRealdataBean);
        } else {
            rainwaterDischargeRealdataBean.setRealdataNo(realdataBean.getRealdataNo());
            rainwaterDischargeRealdataService.updatetRainwaterDischargeRealdataList(rainwaterDischargeRealdataBean);
        }
    }

    public void saveOrUpdatetPublicStormwaterRealdata(PublicStormwaterRealdataBean publicStormwaterRealdataBean) {
        PublicStormwaterRealdataBean realdataBean = stormwaterService.queryByDeviceNo(publicStormwaterRealdataBean.getDeviceNo());
        if (realdataBean == null) {
            stormwaterService.insertPublicStormwaterRealdata(publicStormwaterRealdataBean);
        } else {
            publicStormwaterRealdataBean.setRealdataNo(realdataBean.getRealdataNo());
            stormwaterService.updatePublicStormwaterRealdata(publicStormwaterRealdataBean);
        }
    }

    public void saveOrUpdatetFluggasDischargeRealdata(FluggasDischargeRealdataBean fluggasDischargeRealdataBean) {
        FluggasDischargeRealdataBean realdataBean = fluggasDischargeRealdataService.selectBydeviceNo(fluggasDischargeRealdataBean.getDeviceNo());
        if (realdataBean == null) {
            fluggasDischargeRealdataService.insertRealdata(fluggasDischargeRealdataBean);
        } else {
            fluggasDischargeRealdataBean.setRealdataNo(realdataBean.getRealdataNo());
            fluggasDischargeRealdataService.updateRealdata(fluggasDischargeRealdataBean);
        }

    }

    public void save(String enterpriseNo, int modularNo, String operationType, String recordContent,
                     String operationSource) {
        operationService.save(enterpriseNo, modularNo, operationType, recordContent, operationSource);
    }

    public void save(double cumulative_flow, String enterprise_no, double instantaneous_flow, Date date) {
        flowService.save(cumulative_flow, enterprise_no, instantaneous_flow, date);
    }

    public void drainSave(double cumulative_flow, String enterprise_no, double instantaneous_flow, Date date) {
        drainflowService.save(cumulative_flow, enterprise_no, instantaneous_flow, date);
    }


}
